/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.Medic;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.MedicSyncBroker;
import systole.synchronization.remote.entities.MedicRemote;

/**
 *
 * @author Juan Manuel
 */
public class MedicSyncBrokerDB extends BrokerDB implements MedicSyncBroker {

    public void saveMedicRemote(MedicRemote medicRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote medic");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(medicRemote);
            this.logger.logDebug("remote medic saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save medic remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<MedicRemote> getMedicsToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Medic> getMedicsToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting medic to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select m from MedicRemote mr right join mr.medic m where mr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Medic> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get medic to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los médicos", e.fillInStackTrace());
        }
    }

    public MedicRemote getMedicRemoteByMedic(Medic medic) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting medic remote by local medic");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    MedicRemote.class).add(Restrictions.eq("medic", medic)).setMaxResults(1);
            MedicRemote medicRemote = (MedicRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return medicRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote medic by medic, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los médicos", e.fillInStackTrace());
        }
    }

    public int getLastRemoteMedicIdSynchronized() throws ExceptionDAO {
       try {
            this.logger.logDebug("getting last remote medic sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    MedicRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            MedicRemote medicRemote = (MedicRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (medicRemote != null ? medicRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote medic synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los médicos", e.fillInStackTrace());
        }
    }

    public Medic getMedicByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting medic to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    MedicRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            MedicRemote medicRemote = (MedicRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (medicRemote != null ? medicRemote.getMedic() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get medic by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los médicos", e.fillInStackTrace());
        }
    }
}
